Trò chơi Tic-Tac-Toe, game đánh caro full source code
53.450 lượt xem;
- SmoothSyncMovement.cs
- UtilityScripts /
- Photon Unity Networking /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4 public class SmoothSyncMovement : Photon.MonoBehaviour
5 {
6 public float SmoothingDelay = 5;
7 public void Awake()
8 {
9 if (this.photonView == null || this.photonView.observed != this)
10 {
11 Debug.LogWarning(this + " is not observed by this object's photonView! OnPhotonSerializeView() in this class won't be used.");
12 }
13 }
14
15 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
16 {
17 if (stream.isWriting)
18 {
19 //We own this player: send the others our data
20 stream.SendNext(transform.position);
21 stream.SendNext(transform.rotation);
22 }
23 else
24 {
25 //Network player, receive data
26 correctPlayerPos = (Vector3)stream.ReceiveNext();
27 correctPlayerRot = (Quaternion)stream.ReceiveNext();
28 }
29 }
30
31 private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
32 private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
33
34 public void Update()
35 {
36 if (!photonView.isMine)
37 {
38 //Update remote player (smooth this, this looks good, at the cost of some accuracy)
39 transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * this.SmoothingDelay);
40 transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * this.SmoothingDelay);
41 }
42 }
43
44 }
2 using System.Collections;
3
4 public class SmoothSyncMovement : Photon.MonoBehaviour
5 {
6 public float SmoothingDelay = 5;
7 public void Awake()
8 {
9 if (this.photonView == null || this.photonView.observed != this)
10 {
11 Debug.LogWarning(this + " is not observed by this object's photonView! OnPhotonSerializeView() in this class won't be used.");
12 }
13 }
14
15 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
16 {
17 if (stream.isWriting)
18 {
19 //We own this player: send the others our data
20 stream.SendNext(transform.position);
21 stream.SendNext(transform.rotation);
22 }
23 else
24 {
25 //Network player, receive data
26 correctPlayerPos = (Vector3)stream.ReceiveNext();
27 correctPlayerRot = (Quaternion)stream.ReceiveNext();
28 }
29 }
30
31 private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
32 private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
33
34 public void Update()
35 {
36 if (!photonView.isMine)
37 {
38 //Update remote player (smooth this, this looks good, at the cost of some accuracy)
39 transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * this.SmoothingDelay);
40 transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * this.SmoothingDelay);
41 }
42 }
43
44 }
We own this player: send the others our data
Network player, receive data
private Vector3 correctPlayerPos = Vector3.zero; We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; We lerp towards this
Update remote player (smooth this, this looks good, at the cost of some accuracy)